<?php

namespace App\Http\Controllers\Api;

use App\UserSocialite;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Overtrue\LaravelSocialite\Socialite;

class SocialiteOAuthController extends Controller
{
    protected $backUrl;
    public function __construct()
    {
        $this->backUrl = env('SOCIALITE_JUMP_URL');
    }
    /**
     * Redirect the user to the GitHub authentication page.
     *
     * @param $driver
     * @return mixed
     */
    public function redirectToProvider($driver)
    {
        if (!config('socialite.'.$driver)) {
            return response('Sorry, 请选择其他方式登录...', 403);
        }
        return Socialite::driver($driver)->redirect();
    }

    /**
     * Obtain the user information from GitHub.
     *
     * @param $driver
     * @return mixed
     */
    public function handleProviderCallback(Request $request, $driver)
    {
        // 微博登陆，用户点击取消时，关闭窗口
        if ($driver == 'weibo' && $request->error_uri) {
            return '<script>window.close();</script>';
        }

        $user = Socialite::driver($driver)->user();
        $method = '_' . $driver;
        if (method_exists('App\Http\Controllers\Api\SocialiteOAuthController',$method)) {
            return $this->$method($user, $driver);
        }
        return response('Sorry, 请选择其他方式登录...', 403);
    }

    public function binding (request $request)
    {
        if (!Auth::guard()->attempt(['account' => $request->account, 'password' => $request->password])) {
            return response(['message' => '用户名或者密码错误'], 401);
        }
        $socialite = UserSocialite::getByDriverAndOpenId($request->driver, $request->open_id);
        if (!$socialite) {
            return response(['message' => '页面已过期，请重试'], 400);
        }
        if ($socialite->user) {
            return response(['message' => '该' . $request->driver . '账号已绑定过账号'], 400);
        }
        $user = Auth::user();
        $socialite->user_id = $user->id;
        $socialite->save();
        $access_token = $user->createToken($request->driver . '_' . $user->id . '_' . date('YmdHis'))->accessToken;
        return response(['access_token' => $access_token]);
    }

    /**
     * @param $user
     * @param $driver
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    private function _weibo ($user, $driver) {
        $socialite = UserSocialite::getByDriverAndOpenId($driver, $user['id']);
        if (! $socialite)
            $socialite = UserSocialite::createOne(0, $driver, $user['id'], $user['access_token'], $user['refreshToken'], $user);

        if (!$socialite['user_id'] || !$socialite->user)
            return redirect($this->backUrl . '/binding?driver=' . $driver . '&open_id=' . $user['id'] . '&nickname=' . $user['nickname']);

        // 登陆
        $user = $socialite->user;
        $access_token = $user->createToken($driver . '_' . $user->id . '_' . date('YmdHis'))->accessToken;
        return redirect($this->backUrl . '/login?access_token='. $access_token);
    }

    private function _qq ($user, $driver) {
        $socialite = UserSocialite::getByDriverAndOpenId($driver, $user->id);
        if (! $socialite)
            $socialite = UserSocialite::createOne(0, $driver, $user->id, $user->token->access_token, $user->token->refresh_token, $user);
        if (!$socialite['user_id'] || !$socialite->user)
            return redirect($this->backUrl . '/binding?driver=' . $driver . '&open_id=' . $user->id . '&nickname=' . $user->nickname);
        // 登陆
        $user = $socialite->user;
        $access_token = $user->createToken($driver . '_' . $user->id . '_' . date('YmdHis'))->accessToken;
        return redirect($this->backUrl . '/login?access_token='. $access_token);
    }
}
